package com.njtoyo.taxi.admin.logic.marketing;

import com.njtoyo.taxi.admin.mapper.master.enums.CouponCategory;
import com.njtoyo.taxi.admin.mapper.master.enums.CouponPlatform;
import com.njtoyo.taxi.admin.mapper.master.enums.CouponScope;
import com.njtoyo.taxi.admin.mapper.master.enums.CouponUsageCondition;
import com.njtoyo.taxi.admin.mongo.entity.secondary.AdminGiveCustomerCouponLogMongo;
import com.njtoyo.taxi.admin.rest.presenter.marketing.coupon.AdminGiveCustomerCouponLogListPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.marketing.coupon.AdminGiveLogQueryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

@Service
public class AdminGiveCustomerCouponLogLogic {

    @Autowired
    @Qualifier("secondaryMongoTemplate")
    private MongoTemplate secondaryMongoTemplate;

    public PageImpl<AdminGiveCustomerCouponLogListPresenter> getList(AdminGiveLogQueryDto wrapper) {
        // 构造查询条件
        Query query = new Query();

        Sort sort = Sort.by(Sort.Order.desc("coupon_id"));
        Pageable pageable = PageRequest.of(wrapper.getCurrent(), wrapper.getSize(), sort);

        if (Objects.nonNull(wrapper.getCouponCategory())) {
            query.addCriteria(Criteria.where("category").is(wrapper.getCouponCategory()));
        }

        if (Objects.nonNull(wrapper.getCustomerName())) {
            Pattern customerNamePattern = Pattern.compile(String.format("^.*%s.*$", wrapper.getCustomerName()), Pattern.CASE_INSENSITIVE);
            query.addCriteria(Criteria.where("customer_name").regex(customerNamePattern));
        }

        if (Objects.nonNull(wrapper.getCustomerPhone())) {
            Pattern customerMobileNumberPattern = Pattern.compile(String.format("^.*%s.*$", wrapper.getCustomerPhone()), Pattern.CASE_INSENSITIVE);
            query.addCriteria(Criteria.where("customer_mobile_number").regex(customerMobileNumberPattern));
        }

        if (Objects.nonNull(wrapper.getAdminName())) {
            Pattern adminNamePattern = Pattern.compile(String.format("^.*%s.*$", wrapper.getAdminName()), Pattern.CASE_INSENSITIVE);
            query.addCriteria(Criteria.where("admin_name").regex(adminNamePattern));
        }

        if (Objects.nonNull(wrapper.getAdminPhone())) {
            Pattern adminMobileNumberPattern = Pattern.compile(String.format("^.*%s.*$", wrapper.getAdminPhone()), Pattern.CASE_INSENSITIVE);
            query.addCriteria(Criteria.where("admin_mobile_number").regex(adminMobileNumberPattern));
        }

        //计算总数
        long total = secondaryMongoTemplate.count(query, AdminGiveCustomerCouponLogMongo.class);

        query.with(pageable);
        //查询结果集
        List<AdminGiveCustomerCouponLogMongo> list = secondaryMongoTemplate.find(query, AdminGiveCustomerCouponLogMongo.class);

        List<AdminGiveCustomerCouponLogListPresenter> listDto = new ArrayList<>();

        for (AdminGiveCustomerCouponLogMongo item : list) {
            AdminGiveCustomerCouponLogListPresenter dto = new AdminGiveCustomerCouponLogListPresenter();

            dto.setCustomerId(item.getCustomer_id());
            dto.setCustomerName(item.getCustomer_name());
            dto.setCustomerMobileNumber(item.getCustomer_mobile_number());
            dto.setAdminId(item.getAdmin_id());
            dto.setAdminAccount(item.getAdmin_account());
            dto.setAdminName(item.getAdmin_name());
            dto.setAdminMobileNumber(item.getAdmin_mobile_number());
            dto.setCouponId(item.getCoupon_id());
            dto.setCategory(CouponCategory.getByCode(item.getCategory()));
            dto.setPlatform(CouponPlatform.getByCode(item.getPlatform()));
            dto.setScope(CouponScope.getByCode(item.getScope()));
            dto.setDiscountType(item.getDiscount_type() == 1);
            dto.setDiscountDigit(item.getDiscount_digit());
            dto.setLimitType(CouponUsageCondition.getByCode(item.getLimit_type()));
            dto.setLimitDigit(item.getLimit_digit());
            dto.setCreatedAt(item.getCreated_at());

            listDto.add(dto);
        }

        return new PageImpl<>(listDto, pageable, total);
    }

}
